<template>
  <div class="login">
    <header>
      <div class="logo">
        <router-link to="/home"><img src="../assets/images/logo.png" alt=""></router-link>
      </div>
    </header>
    <div class="main">
      <div class="main-cont">
        <div class="box">
          <div class="login-tit">
            <h2 :class="[titIndex===index?'active':'']" v-for="(item,index) in 2" :key="index" @click="toggleflag(index)"> {{ index===0?'账号登录':'注 册' }} </h2>
            <!-- <router-link to="/login">账号登录</router-link>
            <router-link to="/regist">注 册</router-link> -->
          </div>
          <form action="">
            <label for="username">
              <input type="text" id="username" name="username" placeholder="请输入账号" v-model.trim="user.userName" @focus="foucus" @blur="blur">
            </label>
            <p> <span v-text="userNameErr"></span></p>
            <label for="password">
              <input type="password" id="password" name="password" placeholder="密码" v-model.trim="user.passWord" @focus="foucus" @blur="blur">
            </label>
            <p><span v-text="passWordErr"></span></p>
            <label for="email" v-if="loginOrRegist">
              <input type="text" id="email" name="email" placeholder="邮箱" v-model.trim="user.myEmail" @focus="foucus" @blur="blur">
            </label>
            <p><span v-text="myEmailErr"></span></p>
            <input type="button" id="regist" :value="loginOrRegist?'注册':'登录'">
          </form>
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="footer-box">
        <p> Copyright © 红色阳泉 版权所有 </p>
        <p> All Rights Reserved </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {
        userName: '',
        passWord: ''
      },
      loginOrRegist: false,
      titIndex: 0,
      placeNow: '',
      userNameErr: '',
      passWordErr: '',
      myEmailErr: ''
    }
  },
  methods: {
    foucus(e) {
      this.placeNow = e.target.placeholder
      e.target.placeholder = ''
    },
    blur(e) {
      e.target.placeholder = this.placeNow
      if (e.target.id == 'username') {
        if (this.user.userName.length <= 0) {
          this.userNameErr = '账号不能为空'
        } else if (this.user.userName.length < 6) {
          this.userNameErr = '账号必须8位以上'
        } else {
          this.userNameErr = ''
        }
      }

      if (e.target.id == 'password') {
        if (this.user.passWord.length <= 0) {
          this.passWordErr = '密码不能为空'
        } else if (this.user.passWord.length < 6) {
          this.passWordErr = '密码必须6位以上'
        } else {
          this.passWordErr = ''
        }
      }

      if (e.target.id == 'email') {
        if (regexpEamil.test(this.user.myEmail)) {
          this.myEmailErr = ''
        } else {
          this.myEmailErr = '邮箱格式不正确'
        }
      }
    },
    toggleflag(i) {
      this.titIndex = i
      this.loginOrRegist = !this.loginOrRegist
    }
  }
}
</script>

<style scoped>
body {
  font-family: Arial, sans-serif;
}
header {
  padding-left: 5%;
}
header .logo {
  height: 75px;
  display: flex;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
}
header .logo a {
  display: block;
  height: 90%;
}
header .logo img {
  height: 100%;
}
.main {
  width: 100%;
  height: 80vh;
  background: url(../assets/images/banner2.png) no-repeat;
  background-size: cover;
}
.main-cont {
  position: relative;
  max-width: 1200px;
  height: 100%;
  margin: 0 auto;
}
.box {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-66%);
  width: 400px;
  height: 450px;
  background-color: #ffffff;
  border-radius: 5px;
  box-shadow: 0px 0px 5px #bdbdbd;
}
.box .login-tit {
  display: flex;
  justify-content: space-between;
  width: 80%;
  margin: 0 auto;
  padding: 2vw 3vw;
}
.login-tit h2 {
  font-weight: 600;
  font-size: 20px;
  padding-bottom: 3px;
  color: rgb(131, 131, 131);
  cursor: pointer;
}

.login-tit h2.active {
  color: #000;
  border-bottom: 3px solid #c02929;
}

form {
  display: block;
  width: 80%;
  margin: 0 auto;
  /* padding: 0 10%; */
}

form label {
  position: relative;
  display: block;
  margin-bottom: 5px;
}
form p {
  height: 20px;
  margin: 5px 0;
  padding-left: 5px;
}
form p span {
  display: block;
  font-size: 14px;
  color: red;
}

label[for='username']::before,
label[for='password']::before,
label[for='email']::before {
  font-family: 'iconfont';
  position: absolute;
  top: 50%;
  left: 10px;
  transform: translateY(-50%);
}
label[for='username']::before {
  content: '\e645';
}
label[for='password']::before {
  content: '\e60c';
}

label[for='email']::before {
  content: '\e604';
}

input[name='username'],
input[name='password'],
input[name='email'] {
  width: 100%;
  padding: 10px 0;
  display: block;
  /* line-height: 35px; */
  border: none;
  box-shadow: 0px 0px 3px #979797;
  padding-left: 33px;
}

input[placeholder] {
  font-size: 14px;
  color: #666;
}

input[type='button'] {
  width: 100%;
  padding: 10px;
  border: none;
  background: #b92f2f;
  color: #f5f5f5;
  cursor: pointer;
}
input[type='button']:hover {
  background: #c02929;
}

.footer {
  padding: 25px 0;
  background: none;
  color: #333;
}
.footer .footer-box {
  max-width: 1200px;
  margin: 0 auto;
  text-align: center;
}

/* 移动端样式 */
@media screen and (max-width: 767px) {
  header .logo {
    height: 50px;
  }
  .box {
    width: 80vw;
    height: 50vh;
    min-height: 350px;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .box .login-tit {
    padding: 7vw 7vw 5vw;
  }
  form p {
    height: 17px;
  }
  form p span {
    font-size: 8rem;
  }
  .login-tit a {
    font-size: 14rem;
  }
  footer p {
    font-size: 9rem;
  }
}

/* 平板端样式 */
@media screen and (min-width: 768px) and (max-width: 992px) {
  /* 在此处编写平板端样式 */
  .box {
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .box .login-tit {
    padding: 7vw 7vw 5vw;
  }
}
</style>